package com.example.robertluozizhao.java.test.kotlin

import com.example.robertluozizhao.java.test.kotlin.suspend.TestSuspend
import kotlinx.coroutines.*

/**
 * <pre>
 *     author: luozz@anjiu-tech.com
 *     time  : 2020/10/10
 *     desc  : kotlin 协程
 * </pre>
 */
class KotlinCoroutines {

    companion object {
        private val TAG: String = javaClass.simpleName

        @JvmStatic
        fun main(string: Array<String>) {


//            runBlockingLaunchUsage()

            GlobalScope.launch(Dispatchers.Main){

                println("main:  1.....")
                val member = TestSuspend.INSTANCE.getMember()
                println("main: 4.......")
                withContext(Dispatchers.Main) {
                    println(member.toString())
                }
            }

//            runBlocking {
//
//                println(Thread.currentThread().name)
//                //1. async 接受协程返回结果，切换线程
//                val waitResult = async(Dispatchers.IO){
//                    println("${Thread.currentThread().name}--async")
//                    Thread.sleep(1000)
//                    println("end")
//                    true
//                }
//                println(" waitResult:{$waitResult}")
//                println(Thread.currentThread().name)
//
//
//                val await = waitResult.await()
//                println("waitResult:${await}--${Thread.currentThread().name}")
//
//            }


        }

        /**
         * 1.runBlocking与launch 使用方法
         */
        private fun runBlockingLaunchUsage() {
            runBlocking {
                //1.runBlocking 开启一个协程，此处会阻塞主线程
                println(Thread.currentThread().name)
                launch {
                    //2.在runBlocking内，launch一个协程，此处不会阻塞主线程
                    println("start")
                    Thread.sleep(1000)
                    println(Thread.currentThread().name)
                }
                println("end")
            }
        }
    }


}